// components/load-list/load-list.js
import {getRecordList} from "../../service/Record";
Component({
    /*
    * 接收外部样式
    * */
    options: {
        addGlobalClass: true,
        multipleSlots: true
    },
    /**
     * 组件的属性列表
     */
    properties: {
        api: { //请求接口   （必填）
            type: String,
            value: ""
        },
        params: { //请求参数
            type: Object,
            value: {},
        },
        method: { //请求方式
            type: String,
            value: "GET"
        },
        requestType: { //请求头 contentType
            type: String,
            value: ""
        },
        page: { //第几页 默认第一页
            type: Number,
            value: 1
        },
        pageSize: { //每页个数  默认20个
            type: Number,
            value: 20
        },
        loading: { //是否加载  默认加载
            type: Boolean,
            value: false
        },
        loadingText: { //加载中提示
            type: String,
            value: "加载中..."
        },
        finishedText: { //加载结束提示内容
            type: String,
            value: "没有更多了"
        },
        useFinishedSlot: { //是否打开 完成加载的提示插槽
            type: Boolean,
            value: false,
        },
        errorText: { //加载错误提示内容
            type: String,
            value: "加载失败，点击重新加载"
        },
        reset: { //刷新 重置
            type: Boolean,
            value: false
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        _status: "default", //加载状态   default\loading\err\finished
        _showErrIcon: false //是否在加载内容失败时候显示图标
    },


    /*
    * 数据监听
    * */
    observers: {
        "loading": function (loading) {
            if (loading) {
                if (this.data._status === "default") {
                    this.setData({
                        _status: "loading",
                        reset: false
                    })
                    this.onLoadList("loading");
                }
            }
        },
        "reset": function (reset) {
            if (reset) {
                /*初始化数据*/
                if (this.data._status !== "loading") {
                    this._total = null;
                    this.data.page = 1;
                    this.setData({
                        _status: "loading",
                        loading: false
                    })
                    this.onLoadList("reset");
                }
            }
        }
    },

    /*
    * ready周期函数
    * 不参与渲染的数据
    * */
    ready: function () {
        /*初始化数据*/
        this._total = null; //定义总数 不参与渲染
        this._isLoadList = false; //加载状态
    },



    /**
     * 组件的方法列表
     */
    methods: {
        /*
        * 获取数据
        * */
        onLoadList: async function (type) {
            const that = this;
            try {
                let total = that._total,
                    page = that.data.page,
                    pageSize = that.data.pageSize,
                    params = that.data.params; //传入参数
                params.page = page;
                params.pageSize = pageSize;
                if (!total || total > (page - 1) * pageSize) { //total不存在是第一次请求， total大于是有更多的数据
                    let newData = await getRecordList(params);
                    let newList = newData.list || [];
                    if (newList.length < pageSize) { //当前页面数据 小于 分页长度
                        setTimeout(function () {
                            that.setData({_status: "finished"})
                        }, 200)
                    } else {
                        that.setData({_status: "default"})
                    }
                    that._total = parseInt(newData.total);
                    that.data.page = that.data.page + 1; //不参与渲染
                    that.triggerEvent("loadList", {success: true, result: newList}, {}); //组件接收函数写入数据
                    that.setData({[type]: false});
                } else {
                    that.triggerEvent("loadList", {success: true, result: []}, {});
                    setTimeout(function () {
                        that.setData({
                            [type]: false,
                            _status: "finished"
                        });
                    }, 500)
                }
            } catch (e) {
                console.error(e)
                that.setData({
                    _status: "error",
                    _showErrIcon: that.data.page === 1,
                    [type]: false
                });
                that.triggerEvent("loadList", {success: false, result: null}, {});
            }
        },

        /*
        * 点击加载失败提示，重新加载
        * */
        bindErrTip: function () {
            let loading = this.data.reset;
            if (loading) {
                this.setData({
                    _status: "loading",
                    reset: false
                })
                this.onLoadList("loading");
            } else {
                this.setData({
                    _status: "loading",
                    loading: false
                })
                this.onLoadList("reset");
            }
        }
    }
})
